const a = { val: 1 };
const b = { val: 2 };
const c = { val: 3 };
const d = { val: 4 };
const e = { val: 5 };
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = null;

/**
 * 反转链表
 * @param {object} head 头指针指向元素
 */

const reverseList = function (head) {
  let p1 = head; // 指针1 指向头部 a
  let p2 = null; // 指针2
  /**
   * 保存 b
   * a指针null
   * p2 = p1 {val:1,next:null}
   * p1 = b
   *
   * 保存 c
   * b 指针 {val:1,next:null}
   * p2 = { val: 2 }
   * p1 = c
   *
   * 保存d
   * c指针{ val: 2 }
   * p2 = {val:3}
   * p1 = d
   */
  while (p1) {
    const tmp = p1.next;
    p1.next = p2;
    p2 = p1;
    p1 = tmp;
  }
  return p2;
};
